package com.example.shop_front_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop_front_api.entity.Cart;
import com.example.shop_front_api.entity.Product;
import com.example.shop_front_api.mapper.CartMapper;
import com.example.shop_front_api.service.CartService;
import com.example.shop_front_api.service.ProductService;
import com.example.shop_front_api.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductService productService;

    @Override
    public int insertCart(Cart cart) {
        return cartMapper.insert(cart);
    }

    @Override
    public Cart getCart(Integer memberId, Integer productId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("goods_id", productId);
        return cartMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public int updateCart(Integer memberId, Integer productId, Integer quantity) {
        Product product = productService.getProductById(productId);
        if (product != null) {
            Cart existingCart = getCart(memberId, productId);
            if (existingCart != null) {
                int newQuantity = existingCart.getQuantity() + quantity;
                if (!Tool.checkStock(product.getStock(), product.getLockStock(), newQuantity)) {
                    throw new RuntimeException("库存不足");
                }
            }
        }
        return cartMapper.updateCart(memberId, productId, quantity);
    }

    @Override
    public List<Cart> getCartListByMemberId(Integer memberId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        return cartMapper.selectList(queryWrapper);
    }

    @Override
    public Double getCartTotalAmount(Integer memberId) {
        return cartMapper.getCartTotalAmount(memberId);
    }

    @Override
    public int deleteCartProductById(Integer id) {
        return cartMapper.deleteById(id);
    }

    @Override
    public int updateSelectedById(Long id, Integer selected) {
        return cartMapper.updateSelectedById(id, selected);
    }

    @Override
    public List<Cart> getCartListByMemberIdSelected(Integer memberId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("selected", 1);
        return cartMapper.selectList(queryWrapper);
    }

    @Override
    public int batchUpdateSelected(String ids, Integer selected) {
        return cartMapper.batchUpdateSelected(ids, selected);
    }

    @Override
    @Transactional
    public int updateCartQuantity(Integer id, Integer quantity) {
        Cart cart = cartMapper.selectById(id);
        if (cart != null) {
            Product product = productService.getProductById(cart.getProductId());
            if (product != null && !Tool.checkStock(product.getStock(), product.getLockStock(), quantity)) {
                throw new RuntimeException("库存不足");
            }
        }
        return cartMapper.updateCartQuantity(id, quantity);

    }
}